﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace Yxinmt.Controls
{
    public class Border : BaseControl, IControl
    {
        private System.Windows.Controls.Border _Border { get; set; }

        public Border(Dictionary<string, object> property, IComponent component, bool isFactory = false)
        {
            Property = property;
            _Component = component;
            _Border = new System.Windows.Controls.Border();
            _IsFactory = isFactory;
            if (_IsFactory)
            {
                _FactoryPropertyList = new List<FactoryProperty>();
                _ControlType = typeof(System.Windows.Controls.Border);
            }

            this.Element = _Border;

            InitSet();
            SetProperty();
            SetControlProperty();
        }

        private void SetProperty()
        {
            //this.SetDefaultValue("Radius", 5);
        }

        public override void Add(System.Windows.FrameworkElement control)
        {
            _Border.Child = control;
        }

        public override T GetChild<T>()
        {
            return (T)_Border.Child;
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.Border c = _Border;
            switch (name)
            {
                case "name": c.Name = string.Concat("bdr_", (string)value); SetFactoryProperty("Name", c.Name); break;
                case "Height": c.Height = Convert.ToDouble(value); SetFactoryProperty("Height", c.Height); break;
                case "Width": c.Width =Convert.ToDouble(value); SetFactoryProperty("Width", c.Width); break;
                case "VerticalAlignment": c.VerticalAlignment = GetVerticalAlignment((string)value); SetFactoryProperty("VerticalAlignment", c.VerticalAlignment); break;
                case "HorizontalAlignment": c.HorizontalAlignment = GetHorizontalAlignment((string)value); SetFactoryProperty("HorizontalAlignment", c.HorizontalAlignment); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "GridRowIndex": SetGridRowColumn((string)value); break;
                case "Visible": SetVisible((bool)value); break;
                case "BackColor": c.Background = GetColor((string)value); SetFactoryProperty("Background", c.Background); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
                case "Radius": SetRadius(Convert.ToInt32(value)); break;
                case "Radius2": SetRadius2((string)value); break;
                case "Padding": c.Padding = this.GetThickness((string)value); SetFactoryProperty("Padding", c.Padding); break;
                case "BorderColor": c.BorderBrush = GetColor((string)value); SetFactoryProperty("BorderBrush", c.BorderBrush); break;
                case "BorderThickness": c.BorderThickness = this.GetThickness((string)value); SetFactoryProperty("BorderThickness", c.BorderThickness); break;
                case "ZIndex": SetZIndex((int)value); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex((int)value); break;
            }
        }

        public void SetBorderColor(string color)
        {
            _Border.BorderBrush = GetColor((string)color);
        }

        private void SetRadius(int radius)
        {
            _Border.CornerRadius = new System.Windows.CornerRadius(radius);

            SetFactoryProperty("CornerRadius", _Border.CornerRadius);
        }

        private void SetRadius2(string radius)
        {
            var rs = radius.Split(new char[] { ',', '，' });
            if (rs.Length != 4) return;
            var topLeft = double.Parse(rs[0]);
            var topRight = double.Parse(rs[1]);
            var bottomRight = double.Parse(rs[2]);
            var bottomLeft = double.Parse(rs[3]);
            _Border.CornerRadius = new System.Windows.CornerRadius(topLeft, topRight, bottomRight, bottomLeft);

            SetFactoryProperty("CornerRadius", _Border.CornerRadius);
        }

        protected override void SetFactoryProperty(string name, object value)
        {
            if (!_IsFactory) return;

            switch (name)
            {
                case "Margin": this.AddFactoryProperty(System.Windows.Controls.Border.MarginProperty, value); break;
                case "name": this.AddFactoryProperty(System.Windows.Controls.Border.NameProperty, value); break;
                case "Height": this.AddFactoryProperty(System.Windows.Controls.Border.HeightProperty, value); break;
                case "Width": this.AddFactoryProperty(System.Windows.Controls.Border.WidthProperty, value); break;
                case "FlowDirection": this.AddFactoryProperty(System.Windows.Controls.Border.FlowDirectionProperty, value); break;
                case "VerticalAlignment": this.AddFactoryProperty(System.Windows.Controls.Border.VerticalAlignmentProperty, value); break;
                case "HorizontalAlignment": this.AddFactoryProperty(System.Windows.Controls.Border.HorizontalAlignmentProperty, value); break;
                case "Visibility": this.AddFactoryProperty(System.Windows.Controls.Border.VisibilityProperty, value); break;
                case "Background": this.AddFactoryProperty(System.Windows.Controls.Border.BackgroundProperty, value); break;
                case "CornerRadius": this.AddFactoryProperty(System.Windows.Controls.Border.CornerRadiusProperty, value); break;
                case "Padding": this.AddFactoryProperty(System.Windows.Controls.Border.PaddingProperty, value); break;
                case "BorderBrush": this.AddFactoryProperty(System.Windows.Controls.Border.BorderBrushProperty, value); break;
                case "BorderThickness": this.AddFactoryProperty(System.Windows.Controls.Border.BorderThicknessProperty, value); break;
            }
        }

        public override void EventLoad()
        {
            base.EventLoad();

            bool isClick = Property.GetValue<bool>("IsClick");
            bool isDoubleClick = Property.GetValue<bool>("IsDoubleClick");

            if (isClick || isDoubleClick)
            {
                BindClick(isClick, isDoubleClick);
            }
        }
    }
}
